package com.kgc.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.entity.Major;
import com.kgc.entity.TreeNode;
import com.kgc.entity.TreeResult;
import com.kgc.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("major")
public class MajorServlet {
    @Autowired
    private MajorService majorService;

    @RequestMapping("list")
    @ResponseBody//将方法的返回值转换成json对象返回
    public Map findAll(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                       @RequestParam(value="limit",required = false,defaultValue ="10" ) int limit,
                       Major major
                      ){
        System.out.println(major);
        Map map=new HashMap();
        PageInfo<Major> pb=majorService.findAll(page,limit,major);
        map.put("code",0);
        map.put("msg","OK");
        map.put("count",pb.getTotal());
        map.put("data",pb.getList());
                return map;
    }

    @RequestMapping("save")
    public String save(@RequestParam("file") CommonsMultipartFile file,
                       Major major, HttpSession session)throws Exception{
        majorService.save(major);
        String path=session.getServletContext().getRealPath("image");
        File dir=new File(path);
        if (!dir.exists()){
            dir.mkdir();
        }
        String fileName=System.currentTimeMillis()+file.getOriginalFilename();
        file.transferTo(new File(path+"//"+fileName));
        session.setAttribute("path",fileName);
        return "redirect:/major/list";
    }
    @RequestMapping("save1")
    public String save1(Major major){
        majorService.save(major);
        return "redirect:/major/list";
    }

    @RequestMapping("save2")
    @ResponseBody
    public Map save2(Major major){
        System.out.println(major);
        majorService.saveMajor(major);
        Map map=new HashMap();
        map.put("msg","增加成功");
        return map;
    }
    //单个删除
    @RequestMapping("del/{id}")
    @ResponseBody
    public Map delete(@PathVariable("id") int id) {
        majorService.delete(id);
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","删除成功");
        return map;
    }
    //批量删除
    @RequestMapping("dels")
    @ResponseBody
    public Map delete( int[] ids) {
        majorService.deletes(ids);
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","删除成功");
        return map;
    }
    @RequestMapping("find/{id}")
    @ResponseBody
    public Major findById(@PathVariable("id") int id){
        Major major = majorService.findById(id);
        return major;
    }

    @RequestMapping("update")
    @ResponseBody
    public Map update(Major major){
        System.out.println(major);
        majorService.update(major);
        Map map=new HashMap();
        map.put("msg","修改成功");
        return map;
    }

    @RequestMapping("depts")
    @ResponseBody
    public List<TreeNode> detps(){
       return majorService.findDetps();
    }
    @RequestMapping("depts1")
    @ResponseBody
    public List<TreeResult> Treedetps(){
        return majorService.findTreeResult();
    }
}
